<template>
	<div class="zh-video-library">
		<nav-header></nav-header>
        <!-- 项目配置 -->
        <div class="zh-table">
            <div class="zh-table-open clear">
                <div class="zh-open-left fl">
                    <div class="open-width" style="width: 150px;">
                    视频使用信息表
                    </div>
                </div>
                <div class="zh-open-right fr" v-if="isRoot">
                    <el-switch v-model="isChangeFlag"  @change="switchChange" ></el-switch>
                    <el-button type="primary" @click="configureID" style="margin-left: 20px;">配置ID</el-button>
                </div>
            </div>
            <div
                class="zh-table-contents"
                v-loading="loading.status"
                :element-loading-text="loading.txt">
                <el-table
                :data="projectData"
                border
                max-height="541"
                style="width: 100%"
                :empty-text="hasData">
                <el-table-column
                    prop="name"
                    label="项目名称"
                    width="340"
                    align="center"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column
                    prop="total_num"
                    label="总播放次数"
                    align="center"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column
                    prop="no_used_num"
                    label="剩余播放次数"
                    align="center"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column
                    prop="validity_time"
                    label="有效期"
                    align="center"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column
                    prop=""
                    label="操作"
                    align="center"
                    show-overflow-tooltip>
                    <template >
                        <el-button @click="toLink('/resources/video/play-statistics')" type="text">播放统计</el-button>
                    </template>
                </el-table-column>
                </el-table>
            </div>
        </div>
        <!-- 视频列表 -->
        <div class="zh-table">
            <div class="zh-table-open clear">
                <div class="zh-open-left fl">
                    <div class="open-width" style="width: 120px">
                        <el-select
                            v-model="videoParam.stageId"
                            filterable
                            placeholder="请选择">
                            <el-option
                            v-for="item in orgLevel2"
                            :key="item.xk_id"
                            :label="item.name"
                            :value="item.xk_id"
                            >
                            </el-option>
                        </el-select>
                    </div>
                    <div class="open-width" style="width: 100px">
                        <el-select
                            v-model="videoParam.subjectId"
                            filterable
                            placeholder="请选择">
                            <el-option
                            v-for="item in orgSubject"
                            :key="item.xk_id"
                            :label="item.name"
                            :value="item.xk_id"
                            >
                            </el-option>
                        </el-select>
                    </div>
                    <div class="open-width">
                        <el-input
                        v-model="videoParam.keyword"
                        placeholder="请输入视频名称" @keyup.enter.native="searchVideoList"></el-input>
                    </div>
                    <el-button type="primary" @click="searchVideoList">搜索</el-button>
                    <el-button type="primary" @click="resetData">重置</el-button>
                </div>
            </div>
            <div
                class="zh-table-contents"
                v-loading="loading.status"
                :element-loading-text="loading.txt">
                <el-table
                    :data="videoData"
                    border
                    max-height="541"
                    style="width: 100%">
                    <el-table-column 
                    type="index"
                    :index="typeIndex"
                    label="序号"
                    width="55" 
                    fixed> 
                    </el-table-column>
                    <el-table-column
                        prop="videoTitle"
                        label="视频名称"
                        align="center"
                        show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column
                        prop="stage_name"
                        label="学段"
                        width="200"
                        align="center"
                        show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column
                        prop="subject_name"
                        label="科目"
                        width="200"
                        align="center"
                        show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column
                        prop=""
                        label="操作"
                        width="300"
                        align="center"
                        show-overflow-tooltip>
                        <template slot-scope="scope">
                            <el-button v-if="configureForm.is_try == 1" @click="trySeeVideo(scope.row)" type="text">试看</el-button>
                            <el-button @click="playVideo(scope.row)" type="text">播放</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <div class="table-pagination" v-show="total_number > 10">
                    <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="videoParam.page_index"
                        :page-sizes="[10, 20, 30, 40, 50, 100]"
                        :page-size="videoParam.page_size"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total_number">
                    </el-pagination>
                </div>
            </div>
        </div>

        <el-dialog
        title="提示"
        :visible.sync="configureFlag"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        width="460px"
        >
            <el-form
                :model="configureForm"
                :rules="rules"
                ref="configureForm"
                label-width="80px"
                label-position="left"
                >
                <el-form-item label="主站域名" prop="server_url">
                    <el-input v-model="configureForm.server_url" placeholder="请输入ID"></el-input>
                </el-form-item>
                <el-form-item label="AppID" prop="app_id">
                    <el-input v-model="configureForm.app_id" placeholder="请输入ID"></el-input>
                </el-form-item>
                <el-form-item label="App密钥" prop="app_secret">
                    <el-input v-model="configureForm.app_secret" placeholder="请输入密钥"></el-input>
                </el-form-item>
                <el-form-item label="试看模式">
                    <el-switch v-model="configureForm.is_try" :active-value="1" :inactive-value="2"></el-switch>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="submitConfig(0)">取 消</el-button>
                <el-button type="primary" @click="submitConfig(1)">确 定</el-button>
            </span>
        </el-dialog>
    
        <transition name="el-fade-in-linear">
            <div class="masking" :class="{ activeLive: isActive }">
                <div class="maskingBox">
                    <div class="masking_h">
                        <span>视频预览</span>
                        <i class="el-icon-close" @click="viewClose"></i>
                    </div>
                    <div class="masking_ccc" >
                        <videoAlertView v-if="showVideoPlayer" :playParam.sync="playParam"></videoAlertView>
                    </div>
                </div>
            </div>
        </transition>
	</div>
</template>

<script>
    import videoAlertView from "../../public/videoAlertView";
  	export default {
        components: {
            videoAlertView,
        },
	    data() {
		    return {
                loading: {
                    txt: '暂无数据',
                    status: false,
                },
                projectData: [],
                isChangeFlag: false,
                configureFlag: false,
                configureForm: {
                    server_url: null,
                    app_id: null,
                    app_secret: null,
                    is_try: 2,
                },
                hasData: '--',
                videoData: [

                ],
                rules: {
                    server_url: [
                        {required: true,}
                    ],
                    app_id: [
                        {required: true,}
                    ],
                    app_secret : [
                        {required: true,}
                    ]
                },
                total_number: 0,
                videoParam: {
                    keyword: '',
                    stageId: null, // 学段
                    subjectId: null,
                    page_index: 1,
                    page_size: 10,
                },
                orgLevel2: [],
                orgSubject: [],
                isActive: true,
                showVideoPlayer: false,
                playParam: { play_url: "", type: 'application/x-mpegURL' },
		    }
		},
		created(){
            this.getXkvideo()
            this.getAppInfo(1)
            this.getOrgLevel2()
            this.getSubject()
            this.getVideoList()
        },
        watch: {

        },
        computed: {
            isRoot() {
                return JSON.parse(sessionStorage.getItem('user_info_str')).check_rolebaseid == 1
            }
        },
		methods: {
            // 整个页面默认状态下，仅root可见，开启以后管理员可见
            getXkvideo() {
                const success = res =>{
                    this.isChangeFlag = res.data == 1 ? true : false
                }
                this.$ajax.get('/api/xkvideo/xkvideo_show', {func: {success}})
            },
            switchChange(val) {
                if(val){
                    this.submitSwitchVal(1)
                    this.$message.success('操作成功')
                }else {
                    let yes = (data) => {
                        this.submitSwitchVal(2)
                    };
                    let cancel = (data) => {
                        this.isChangeFlag = true
                    };
                    this.e_confirm({
                        yes: yes,
                        cancel: cancel,
                        txt: "关闭以后页面仅root用户才可见，是否要关闭？",
                    });
                }
            },
            submitSwitchVal(num) {
                const success = res =>{
                    this.getXkvideo()
                }
                let params = {
                    status: num
                }
                this.$ajax.put('/api/xkvideo/xkvideo_show', params,{func: {success}})
            },
            // 配置ID
            configureID() {
                this.configureFlag = true
                this.getAppInfo(0)
            },
            getAppInfo(flag){
                const success = res =>{
                    if(!res.data.app_id) {
                        this.hasData = '暂无数据，请先配置ID'
                    }else {
                        flag && this.getProjectData(res.data)
                    }
                    this.configureForm = res.data
                }
                this.$ajax.get('/api/xkvideo/app_config', {func: {success}})
            },
            submitConfig(flag) {
                if(flag) {
                    this.configureFlag = false
                    this.editAppInfo()
                }else{
                    this.configureFlag = false
                }
            },
            editAppInfo() {
                const success = res =>{
                    this.getProjectData(res.data)
                }
                this.$ajax.put('/api/xkvideo/app_config', this.configureForm, {func: {success}})
            },

            getProjectData(params) {
                this.projectData = []
                const success = res =>{
                    this.projectData.push(res.data)
                }
                const fail = res  =>{
                    this.hasData = res.msg
                }
                this.$ajax.get('/api/xkvideo/video_app_config', {params, func: {success, fail}})
            },
            toLink(link) {
                this.$router.push(link);
            },
            trySeeVideo(obj){
                this.getTrySeeVideoUrl(obj.xk_id)
            },
            getTrySeeVideoUrl(xk_id) {
                const success = res => {
                    if(res.data.ext == 'mp4') {
                        this.playParam.type = 'video/mp4'
                    }else{
                        this.playParam.type = 'application/x-mpegURL'
                    }
                    this.playParam.play_url = res.data.play_url
                    this.showVideoPlayer = true;
                    this.isActive = false;
                }
                let params = {
                    xk_id: xk_id,
                    user_id: this.$g.user_id,
                }
                this.$ajax.get('/api/xkvideo/preview_video_playurl', {params, func: {success}})
            },
            playVideo(obj) {
                this.getXkvideoPlayurl(obj.xk_id)
            },
            getXkvideoPlayurl(xk_id) {
                const success = res => {
                    if(res.data.ext == 'mp4') {
                        this.playParam.type = 'video/mp4'
                    }else{
                        this.playParam.type = 'application/x-mpegURL'
                    }
                    this.playParam.play_url = res.data.play_url
                    this.showVideoPlayer = true;
                    this.isActive = false;
                }
                let params = {
                    xk_id: xk_id,
                    user_id: this.$g.user_id,
                }
                this.$ajax.get('/api/xkvideo/video_playurl', {params, func: {success}})
            },
            getOrgLevel2() {
                let success = (data) => {
                    this.orgLevel2 = data.data;
                    this.orgLevel2.unshift({ xk_id: null, name: "全部学段" });
                };
                this.$ajax.get("/api/xkvideo/stages_list", {func: { success: success }});
            },
            getSubject() {
                let success = (data) => {
                    this.orgSubject = data.data;
                    this.orgSubject.unshift({ xk_id: null, name: "全部科目" });
                };
                this.$ajax.get("/api/xkvideo/subjects_list", {func: { success }});
            },
            searchVideoList() {
                this.videoParam.page_index = 1
                this.videoParam.page_size = 10
                this.getVideoList()
            },
            resetData() {
                this.videoParam = {
                    keyword: '',
                    stageId: null, // 学段
                    subjectId: null,
                    page_index: 1,
                    page_size: 10,
                }
                this.getVideoList()
            },
            getVideoList() {
                let success = (data) => {
                    this.videoData = data.data.data
                    this.total_number = data.data.data_sum
                };
                this.$ajax.get("/api/xkvideo/video_list",{params: this.videoParam, func: { success }});
            },

            // 序号
            typeIndex(index) {
                return (
                    index + (this.videoParam.page_index - 1) * this.videoParam.page_size + 1
                );
            },
            handleSizeChange(val) {
                this.videoParam.page_index = 1
                this.videoParam.page_size = val
                this.getVideoList()
            },
            handleCurrentChange(val) {
                this.videoParam.page_index = val
                this.getVideoList()
            },
            viewClose() {
                this.showVideoPlayer = false;
                this.isActive = true;
            },
		},
  	}
</script>

<style lang="scss">
	.zh-video-library {
        .masking {
            display: block;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            transition: 0.5s;
            background: rgba(0, 0, 0, 0.3);
            z-index: 4;
            .maskingBox {
                position: absolute;
                top: 20%;
                left: 50%;
                width: 800px;
                margin-left: -400px;
                background: #fff;
            }
            .masking_h {
                padding: 10px 15px;
                background: #348efc;
                text-align: center;
                text-align-last: justify;
                > span,
                > i {
                font-size: 16px;
                text-align-last: initial;
                display: inline-block;
                vertical-align: middle;
                color: #fff;
                }
                > i {
                cursor: pointer;
                }
            }
            .masking_ccc {
                padding: 15px;
                height: 360px
            }
        }
        .activeLive {
            opacity: 0;
            visibility: hidden;
        }
	}
</style>